#include <stdio.h>

#include <pthread.h>

void pthread_clean_call(void *arg)
{
    char *str = (char *)arg;
    printf("pthread clean exec (%s)\n", str);
}

void *th_func(void *arg)
{
    int execflag = (int)arg;

    pthread_cleanup_push(pthread_clean_call, "first clean func!");
    pthread_cleanup_push(pthread_clean_call, "second clean func!");

    printf("thread runnig %lx\n", pthread_self());

    pthread_cleanup_pop(execflag);
    pthread_cleanup_pop(execflag);

    return (void *)0;
}

int main()
{
    pthread_t pid1, pid2;

    if (pthread_create(&pid1, NULL, th_func, (void *)0) != 0)
    {
        perror("pthread_create error!");
    }

    if (pthread_create(&pid2, NULL, th_func, (void *)1) != 0)
    {
        perror("pthread_create error!");
    }

    pthread_join(pid1, NULL);
    printf("pthread(%lx) finished\n",pid1);
    pthread_join(pid2, NULL);
    printf("pthread(%lx) finished\n",pid2);

    return 0;
}